import sys
from xml.sax import make_parser 
from xml.sax.handler import ContentHandler
        
class JD_Tag:
    def __init__(self,level,isCurrent=False):
        self.level = level
        self.isCurrent = isCurrent

class JD_Handler(ContentHandler):
    def __init__(self,basic={}):
        #essential info
        self.basic = basic
        self.tags = []
        self.depth = 0
        self.chars = []
        
    def in_tag(self, tag):
        self.tags.append(tag)
        self.depth = len(self.tags)
        if self.basic.has_key(tag) :
            self.basic[tag].isCurrent = True

    def out_tag(self, tag):
        if self.basic.has_key(tag) :
            self.basic[tag].isCurrent = False
        self.tags.pop()
        self.depth = len(self.tags)
    
    def expect_tag(self, expect_tag):
        try:
            JD_Tag = self.basic[expect_tag]
            if JD_Tag:
                return expect_tag==self.tags[-1] and (self.depth==JD_Tag.level) and (JD_Tag.isCurrent)
##                #debug code
##                if JD_Tag and not bool_value:
##                    if not (expect_tag==self.tags[self.depth-1]):
##                        print 'Match_Err: expct:%s, actul:%s' % (expect_tag,self.tags[self.depth-1])
##                        pass
##                    elif not (self.depth==JD_Tag.level):                    
##                        print 'Level_Err: %s. expct:%d, actul:%d' % (expect_tag,JD_Tag.level,self.depth)
##                        pass
##                    elif not (JD_Tag.isCurrent):
##                        print 'Current_Err: %s is not current' % (expect_tag)
##                    else:
##                        print '-_-_Err: %s' % (expect_tag)
##                        pass
##                return bool_value
            return False
        except:
            return False
